package com.lxy.demo.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DelCookieServlet", urlPatterns = "/DelCookieServlet")
public class DelCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            //匹配本地的cookie
            if (cookie.getName().equals("username")){
                //1.设置cookie路径
                cookie.setPath(request.getContextPath());
                //2.设置cookie有效期为0
                cookie.setMaxAge(0);
                //3.响应到浏览器上
                response.addCookie(cookie);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
